home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15544 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.cern.ch!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is wrong with this loop?
  5. Date: 19 Apr 96 20:09:05 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.829944545@news.cern.ch>
  8. References: <4l86la$1t9@uwm.edu> <4l8lt7$d9h@HOPPER.ACM.ORG>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=US-ASCII
  12. Content-Transfer-Encoding: 7bit
  13. X-Newsreader: NN version 6.5.0 #18 (NOV)
  14.  
  15. In <4l8lt7$d9h@HOPPER.ACM.ORG> varnk@e62.diebold.com (Ken Varn) writes:
  16.  
  17. >In article <4l86la$1t9@uwm.edu>, mardavuy@alpha2.csd.uwm.edu says...
  18. >/>
  19. >/>#include <stdio.h>
  20. >/>int main(void)
  21. >/>{
  22. >/>   int dia;
  23. >/>
  24. >/>   char cd;
  25. >/>
  26. >/>   ...
  27. >/>
  28. >/>   scanf("%c", &cd);
  29. >/>   while (cd != 'm' || cd ! 'f' || cd != 'o')
  30. >/>        {
  31. >/>        printf("Re-enter m, f, or o.\n");
  32. >/>        scanf(%c", &cd);
  33. >/>        }
  34. >/>   ...
  35. >/>}
  36. >/>
  37. >/>It works fine, except that it gives me the printf() twice.
  38. >
  39. >
  40. >Try changing your scanf() as follows:
  41. >
  42. >scanf("%c ",&cd);
  43.  
  44. Did you actually try your advice?  Obviously not!
  45.  
  46. >This tells scanf() to read past any white space characters after the 
  47. >charadter read.
  48.  
  49. And this is why your solution is brain dead.  scanf cannot know that it
  50. has read all the whitespace _before_ reading a non-whitespace character.
  51. Which means that the scanf call will block until the user types _two_
  52. lines of text, which is definitely not what the original poster wanted or
  53. what the user of the program would expect.
  54.  
  55. Dan
  56. --
  57. Dan Pop
  58. CERN, CN Division
  59. Email: danpop@mail.cern.ch 
  60. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  61.